package com.px.project.module.ht.controller;

import com.px.common.dto.BaseBodyDto;
import com.px.common.dto.BaseQueryDto;
import com.px.common.util.DictUtil;
import com.px.project.module.ht.dto.ConstructionProgressQueryDto;
import com.px.project.module.ht.dto.ConstructionProgressUpdateDto;
import com.px.project.module.ht.entity.ConstructionProgress;
import com.px.project.module.ht.service.ConstructionProgressService;
import com.px.project.module.ht.vo.ConstructionProgressVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 系统管理》生产调度》施工进度管理接口
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/ht/construction_progress")
public class ConstructionProgressController {
    private final ConstructionProgressService constructionProgressService;

    /**
     * 分页查询
     *
     * @param pageVo 分页
     * @param constructionProgressQuery 施工进度
     * @return {@link Page}<{@link ConstructionProgressVo}> 列表
     */
    @GetMapping(value = "/page")
    public Page<ConstructionProgressVo> page(@Validated BaseQueryDto<ConstructionProgressVo> pageVo,
        ConstructionProgressQueryDto constructionProgressQuery) {
        return constructionProgressService.page(pageVo, constructionProgressQuery);
    }

    /**
     * 保存
     *
     * @param constructionProgress 施工进度管理
     * @return {@link Boolean} 状态标识
     */
    @PostMapping(value = "/save")
    public Boolean save(@RequestBody ConstructionProgress constructionProgress) {
        if (ObjectUtil.isNotEmpty(constructionProgress.getStartMileageKm()) && ObjectUtil.isNotEmpty(constructionProgress.getStartMileageM())
            && ObjectUtil.isNotEmpty(constructionProgress.getEndMileageKm()) && ObjectUtil.isNotEmpty(constructionProgress.getEndMileageM())) {
            DictUtil.CheckStartAndEndDk(constructionProgress.getStartMileageKm(), constructionProgress.getStartMileageM(),
                constructionProgress.getEndMileageKm(), constructionProgress.getEndMileageM());
        }
        return constructionProgressService.save(constructionProgress);
    }

    /**
     * 更新
     *
     * @param constructionProgress 施工进度管理
     * @return {@link Boolean }
     */
    @PostMapping("/update")
    public Boolean update(@RequestBody ConstructionProgressUpdateDto constructionProgress) {
        if (ObjectUtil.isNotEmpty(constructionProgress.getStartMileageKm()) && ObjectUtil.isNotEmpty(constructionProgress.getStartMileageM())
            && ObjectUtil.isNotEmpty(constructionProgress.getEndMileageKm()) && ObjectUtil.isNotEmpty(constructionProgress.getEndMileageM())) {
            DictUtil.CheckStartAndEndDk(constructionProgress.getStartMileageKm(), constructionProgress.getStartMileageM(),
                constructionProgress.getEndMileageKm(), constructionProgress.getEndMileageM());
        }
        return constructionProgressService.updateById(constructionProgress);
    }

    /**
     * 删除
     *
     * @param baseBodyDto 请求body基础vo
     * @return {@link Boolean }
     */
    @PostMapping("/delete")
    public Boolean delete(@RequestBody @Validated BaseBodyDto baseBodyDto) {
        return constructionProgressService.removeByIds(baseBodyDto.getIdList());
    }
}